//
//  YbrUserPlayListDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-5.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrUserPlayListItem.h"
#import "YbrUserPlayListDao.h"
#import "YbrMusicItem.h"
#import "YbrTimerUtil.h"

@implementation YbrUserPlayListDao

-(BOOL) removePlaylistById:(NSUInteger) _id {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from user_playlist where _id=%i",(int)_id];
        isOk = [db executeUpdateWithFormat:@"delete from user_playlist_localmusic where playlistid=%i",(int)_id];
    }];
    return isOk;

}
-(BOOL) removePlayMusicById:(int)playId musicId:(int)muiscId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from user_playlist_localmusic where playlistid=%i and musicid=%i",playId ,muiscId];
    }];
    return isOk;
}
// 这里放置的是Playlist对象
-(NSArray *) getAllPlayList {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from user_playlist"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrUserPlayListItem *item = [[YbrUserPlayListItem alloc] init];
            item._id = [rs intForColumn:@"_id"];
            item.sort = [rs intForColumn:@"sort"];
            item.name = [rs stringForColumn:@"name"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
}

-(int) getMusicNumById:(int)playlistId {
    __block int num;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select count(*) from user_playlist_localmusic where playlistid=%i" ,playlistId];
        
        while (rs.next) {
            num = [rs intForColumn:@"num"];
            break;
        }
        [rs close];
    }];
    return num;
}

// 添加PlayList 并且将插入后生成的ID返回回来
-(int) addPlaylist:(NSString *)name {
    __block int playId = -1;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"insert into user_playlist (name) VALUES (?)", name];
        
        FMResultSet *rs;
        rs = [db executeQueryWithFormat:@"select _id from user_playlist where name=%@" ,name];
        
        while (rs.next) {
            playId = [rs intForColumn:@"_id"];
            break;
        }
        [rs close];
    }];
    
    return playId;
}

// 这里的NsArray里面放置的是Music对象
-(NSArray *) getMusicListById:(int) playListId {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from user_playlist_localmusic where playlistid=%i" ,playListId];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrMusicItem *item = [[YbrMusicItem alloc] init];
            item.musicId = [rs intForColumn:@"musicid"];
            item.musicName = [rs stringForColumn:@"name"];
            item.singername = [rs stringForColumn:@"singername"];
            item.albumname = [rs stringForColumn:@"albumname"];
            item.downloadurl = [rs stringForColumn:@"downloadurl"];
            item.file = [rs stringForColumn:@"file"];
            item.img = [rs stringForColumn:@"img"];
            item.duration = [rs intForColumn:@"duration"];
            item.type = [rs intForColumn:@"type"];
            item.timeStr = [rs stringForColumn:@"timerstr"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
}

-(NSString *) getPlaylistNameById:(int) playListId {
    __block NSString *playlistName;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select name from user_playlist where _id=%i" ,playListId];
        
        while (rs.next) {
            playlistName = [rs stringForColumn:@"name"];
            break;
        }
        [rs close];
    }];
    
    return playlistName;
}

-(BOOL) renamePlayList:(int) playListId name:(NSString *)name {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"update user_playlist set name = %@ where _id=%i" ,name ,playListId];
    }];
    return isOk;
}

// 这里是Integter对象 (是playlist的ID)
-(NSArray *) getHaveIdsPlaylist:(int) musicId {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select playlistid from user_playlist_localmusic where musicid=%i" ,musicId];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            NSNumber *playlistid = [NSNumber numberWithInt:[rs intForColumn:@"playlistid"]];
            [tempArr addObject:playlistid];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    return arr;
}

-(BOOL) setMusicToPlaylistByClassInfoItem:(int) playlist classInfo:(YbrClassInfoItem *) classInfo {
    __block BOOL isOk = false;

    
    return isOk;
}
-(BOOL) setMusicToPlaylistByHttpMyBagItem:(int) playlist httpMyBagItem:(YbrHttpMyBagItem *) httpMyBag {
    
    return false;
}

-(BOOL) isHavePlaylistName:(int) musicid playlistId:(int) playlistId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select playlistid from user_playlist_localmusic where musicid=%i and playlistid=%i" ,musicid ,playlistId];
        
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;

}

-(BOOL) isHaveName:(NSString *) name {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select playlistid from user_playlist where name=%@" ,name];
    
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
}
// 获得不重复的名称
-(NSString *) getPlaylistNameNoRep {
    __block NSString *playlistName;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        NSString *name;
        BOOL flag = YES;
        NSUInteger position = 1;
        
        while (flag) {
            name = [NSString stringWithFormat:@"%@%i" ,@"播放列表" ,position];
            FMResultSet *rs;
            // 1.查询数据
            rs = [db executeQueryWithFormat:@"select name from user_playlist where name=%@" ,name];
            
            if(!rs.next) {
                playlistName = name;
                break;
            }
            position ++;
            [rs close];
        }
        
    }];
    
    return playlistName;
}

-(BOOL) setMusicToPlaylistByClassInfoItem:(int) playlist classInfo:(YbrClassInfoItem *) classInfo courseName:(NSString *)courseName channelName:(NSString *) channelName img:(NSString *) img {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"insert into user_playlist_localmusic (playlistid ,name, singername ,albumname ,downloadurl ,img ,duration ,type ,timerstr ,musicid) VALUES (?,?,?,?,?,?,?,?,?,?)",
                [NSNumber numberWithInt:playlist] ,
                classInfo.name,
                channelName ,
                courseName ,
                classInfo.audio_url ,
                img,
                [NSNumber numberWithInt:(int)[YbrTimerUtil makeTimeInt:classInfo.timelength]],
                [NSNumber numberWithInt:Type_Http_Source],
                classInfo.timelength ,
                [NSNumber numberWithInt:classInfo.ID]];
    }];
    return isOk;
}

-(BOOL) setMusicToPlaylistByMusicHall:(int) playlist classInfo:(YbrClassInfoItem *) classInfo ybrchannelName:(NSString *)ybrchannelName {
    __block BOOL isOk = false;

    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"insert into user_playlist_localmusic (playlistid ,name, singername ,albumname ,downloadurl ,duration ,type ,timerstr ,musicid) VALUES (?,?,?,?,?,?,?,?,?)",
                [NSNumber numberWithInt:playlist] ,
                classInfo.name,
                ybrchannelName ,
                @"音伴尔" ,
                classInfo.audio_url ,
                [NSNumber numberWithInt:(int)[YbrTimerUtil makeTimeInt:classInfo.timelength]],
                [NSNumber numberWithInt:Type_Http_Source],
                classInfo.timelength ,
                [NSNumber numberWithInt:classInfo.ID]];
    }];
    return isOk;
}

-(BOOL) setMusicToPlaylistByLocalMusic:(int) playlist musicItem:(YbrMusicItem *) musicItem {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdate:@"insert into user_playlist_localmusic (playlistid ,name, singername ,albumname,file ,downloadurl ,duration ,type ,timerstr ,musicid) VALUES (?,?,?,?,?,?,?,?,?,?)",
                [NSNumber numberWithInt:playlist] ,
                musicItem.musicName ,
                musicItem.singername ,
                musicItem.albumname ,
                musicItem.file,
                musicItem.downloadurl ,
                [NSNumber numberWithInt:musicItem.duration],
                [NSNumber numberWithInt:musicItem.type] ,
                musicItem.timeStr ,
                [NSNumber numberWithInt:musicItem.musicId]];
    }];
    return isOk;
}


@end
